﻿Ajax = {
    ClientData: function (key, value) {
        this.key = key;
        this.value = value;
    },
    jsonSerializeObject: function (_obj) {
        if (_obj != undefined && typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined') {
            return JSON.stringify(_obj);
        }
        switch (typeof _obj) {
            case 'number':
            case 'boolean':
            case 'function':
                return _obj;
                break;
            case 'string':
                return '"' + _obj.replace(/"/mg, "'") + '"';
                break;
            case 'object':
                var str;
                if (_obj.constructor === Array || typeof _obj.callee !== 'undefined') {
                    str = '[';
                    var i, len = _obj.length;
                    for (i = 0; i < len - 1; i++) { str += AjaxUtil.jsonSerializeObject(_obj[i]) + ','; }
                    str += AjaxUtil.jsonSerializeObject(_obj[i]) + ']';
                } else {
                    str = '{';
                    var key;
                    for (key in _obj) { str += key + ':' + AjaxUtil.jsonSerializeObject(_obj[key]) + ','; }
                    str = str.replace(/\,$/, '') + '}';
                }
                return str;
                break;
            default:
                return '""';
                break;
        }
    },
    convertArrayClientDataToQueryString: function (arrClientData) {
        var rs = "";
        $.each(arrClientData, function (i, obj) {
            var jsonNode = "";
            jsonNode = obj.key + "=" + obj.value;
            rs += jsonNode + "&";
        });
        if (rs != "") {
            rs = rs.substring(0, rs.length - 1);
        }
        rs = "?" + rs;
        return rs;
    },
    callWebMethod: function (timeout, pagemethod, params, onSuccess, onError, onBeforeSend, onComplete) {
        var href = window.location.href;
        var index = href.indexOf("?", 0);
        if (index > 0) { href = href.substring(0, index); }

        if (timeout) {
            // set default timeout to 5s
            timeout = 5000;
        }

        var dataJson = this.jsonSerializeObject(params);

        $.ajax({
            timeout: timeout,
            type: "POST",
            url: href + "/" + pagemethod,
            data: dataJson,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: onBeforeSend,
            success: function (msg) { onSuccess(msg.d); },
            error: onError,
            complete: onComplete
        });
    },
    callPage: function (timeout, href, params, onSuccess, onError, onBeforeSend, onComplete) {
        var index = href.indexOf("?", 0);
        if (index > 0) { href = href.substring(0, index); }

        if (timeout) {
            // set default timeout to 5s
            timeout = 5000;
        }

        var querystring = this.convertArrayClientDataToQueryString(params);
        href += querystring;

        $.ajax({
            timeout: timeout,
            url: href,
            beforeSend: onBeforeSend,
            success: function (msg) { onSuccess(msg); },
            error: onError,
            complete: onComplete
        });
    }
}
Temp = {
    /******************************************/
    ClientData: function (key, value, age) {
        this.name = key;
        this.address = value;
        this.age = age;
    },
    getObjectFromJson: function (json) {
        var rs = new Array();
        var objs = jQuery.parseJSON(json);
        $.each(objs, function (i, obj) {
            var a = new Temp.ClientData(obj['name'], obj['address'], obj['age']);
            rs.push(a);
        });
        return rs;
    },
    doBackgroundPostBack: function (errormessage) {
        var arrParams = new Array();
        var a = new Temp.ClientData("KhaiNX", "294 BD", 15);
        arrParams.push(a);

        var b = new Temp.ClientData("Nguyen Xuan Khai", "123 Q1", 24);
        arrParams.push(b);

        var dataJson = AjaxUtil.jsonSerializeObject(arrParams);
        // show loading
        Common.showLoader();

        callServer(dataJson, null);
    },
    receiveServerData: function (arg, context) {
        var rs = jsTemplate.getObjectFromJson(arg);
        $('#button1').val(rs[0].name);
        // close loading
        Common.hideLoader();
    },
    processCallBackError: function (arg, context) {
        $('#button1').val('An error has occurred.');
        // close loading
        Common.hideLoader();
    }
};